package com.lagou.edu.controller;

import com.lagou.edu.pojo.Result;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */
    @Autowired
    private ResumeService resumeService;

    public static final String ADMIN_NAME = "admin";
    public static final String ADMIN_PWD = "admin";

    /**
     * 登录页
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    /**
     * 登录action
     * @param username
     * @param password
     * @param request
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView login(String username, String password, HttpServletRequest request, ModelAndView modelAndView){
        if (ADMIN_NAME.equals(username) && ADMIN_PWD.equals(password)) {
            HttpSession session = request.getSession();
            session.setAttribute("loginUser", username);
        } else {
            modelAndView.addObject("message", "用户名或密码错误");
            modelAndView.setViewName("login");
            return modelAndView;
        }
        modelAndView.setViewName("redirect:/admin/resume/list");
        return modelAndView;
    }

    /**
     * 账户列表页
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/resume/list",method = RequestMethod.GET)
    public ModelAndView resumeList(ModelAndView modelAndView) throws Exception {
        List<Resume> accounts = resumeService.queryAccountList();
        modelAndView.addObject("accountList", accounts);
        modelAndView.setViewName("resumeList");
        return modelAndView;
    }

    /**
     * 账户新增/修改
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/resume", method = RequestMethod.POST)
    @ResponseBody
    public Result modify(Resume resume) throws Exception {
        if (resume.getId() == 0){
            Resume oldResume = resumeService.findByName(resume.getName());
            // 名称相同则失败
            if (oldResume != null && StringUtils.equals(oldResume.getName(), resume.getName())) {
                return Result.error("名称相同");
            }
            Resume newResume = resumeService.save(resume);
            if (newResume != null && newResume.getId() != 0) {
                return Result.success(newResume, "添加成功");
            } else {
                return Result.error("添加失败");
            }
        } else {
            Resume newResume = resumeService.save(resume);
            if (newResume != null && newResume.getId() != 0) {
                return Result.success(newResume, "修改成功");
            }
            return Result.error("修改失败");
        }
    }

    /**
     * 账户删除  DELETE方法无法传参，所以只能将参数放在url路径上,使用restful传参
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/resume/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@PathVariable("id") Long id) throws Exception {
        if (id !=null && resumeService.findById(id) != null){
            resumeService.deleteById(id);
            return Result.success(null,"删除成功");
        }
        return Result.error("该账户不存在或已经删除");
    }
}
